home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / DTS QT Utilities.Aug-95 / Projects & Test Apps / Drag&Drop Shell / Tool Framework / ToolFramework.c next >
Encoding:
C/C++ Source or Header  |  1995-04-02  |  6.3 KB  |  248 lines  |  [TEXT/MPCC]

  1. /*
  2.     File:        ToolFramework.c
  3.  
  4.     Contains:    Simple AE framework for QuickTime related tools.
  5.  
  6.     Written by:    DTS
  7.  
  8.     Copyright:    © 1995 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.        <1>         1/30/95    khs        first file
  13.        
  14. */
  15.  
  16.  
  17. // INCLUDES
  18. #include "ToolFramework.h"
  19. #include "DTSQTUtilities.h"
  20. #include "TestFunction.h"
  21.  
  22. // GLOBALS AND CONSTANTS
  23. Boolean gOneShot = true;            // Will we trigger this application just once, or is it OK to keep the app open (need 
  24.                                                 // a later quit AE message then.
  25.  
  26. Boolean gDone = false;
  27. unsigned long gWNEsleep = 0;        
  28. Boolean gHasAppleEvents = false;
  29.  
  30.  
  31. // ______________________________________________________________________
  32. // MAIN
  33. void main(void)
  34. {    
  35.     OSErr anErr;
  36.         
  37.     InitMacEnvironment(10L);        
  38.  
  39.     if (!InitializeAppleEvents())
  40.         ExitToShell();
  41.         
  42.     if( !QTUIsQuickTimeInstalled() )
  43.         ExitToShell();
  44.  
  45. #if powerc    
  46.     if( !QTUIsQuickTimeCFMInstalled() )
  47.         ExitToShell();                                
  48. #endif 
  49.  
  50.     anErr = EnterMovies(); DebugAssert(anErr == noErr);
  51.     if(anErr != noErr)
  52.         ExitToShell();
  53.  
  54.     MainEventLoop();
  55. }
  56.  
  57.  
  58. // ______________________________________________________________________
  59. pascal void InitMacEnvironment(long nMasters)
  60. {
  61.     long    i;
  62.     MaxApplZone();
  63.     
  64.     for(i = 0; i <nMasters; i++)
  65.         MoreMasters();
  66.     
  67.     InitGraf(&qd.thePort);
  68.     InitFonts();
  69.     InitWindows();
  70.     InitMenus();
  71.     FlushEvents(everyEvent, 0);
  72.     TEInit();
  73.     InitCursor();
  74.     InitDialogs(NULL);
  75. }
  76.  
  77.  
  78. // ______________________________________________________________________
  79. pascal Boolean InitializeAppleEvents(void)
  80. {
  81.     OSErr     anErr;
  82.     long        aVersion;
  83.     
  84.     anErr = Gestalt(gestaltAppleEventsAttr, &aVersion); DebugAssert(anErr == noErr);
  85.     if(anErr != noErr)
  86.         return false;        // Apple Event Manager is not present on the system.
  87.         
  88.     if( !(aVersion & (1L << gestaltAppleEventsPresent)))
  89.         return false;        // The current configuration does not support Apple Events.
  90.         
  91.     // Continue installing our core event handlers.
  92.     gHasAppleEvents = true;
  93.  
  94.     anErr = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, 
  95.                                                     NewAEEventHandlerProc(AEOpenHandler), 0, false);
  96.     DebugAssert(anErr == noErr);
  97.     if(anErr)
  98.         return false;
  99.  
  100.     anErr = AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, 
  101.                                                     NewAEEventHandlerProc(AEOpenDocHandler), 0, false);
  102.     DebugAssert(anErr == noErr);
  103.     if(anErr)
  104.         return false;
  105.  
  106.     anErr = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
  107.                                                      NewAEEventHandlerProc(AEQuitHandler), 0, false);
  108.     DebugAssert(anErr == noErr);
  109.     if(anErr)
  110.         return false;
  111.  
  112.     anErr = AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, 
  113.                                                     NewAEEventHandlerProc(AEPrintHandler), 0, false);
  114.     DebugAssert(anErr == noErr);
  115.     if(anErr)
  116.         return false;
  117.             
  118.     return true;
  119. }
  120.  
  121.  
  122. // ______________________________________________________________________
  123. pascal void MainEventLoop(void)
  124. {
  125.     EventRecord anEvent;
  126.     
  127.     while(!gDone)
  128.     {
  129.         SystemTask();
  130.         WaitNextEvent(everyEvent, &anEvent, gWNEsleep, NULL);
  131.         
  132.         switch(anEvent.what)
  133.         {
  134.         // We are only interested in high level events.
  135.             case kHighLevelEvent:
  136.                 if(gHasAppleEvents)
  137.                     AEProcessAppleEvent(&anEvent);
  138.                     break;
  139.             
  140.             default:
  141.                 DebugAssert("we should not get any events here");
  142.                 break;
  143.         }
  144.     }
  145. }
  146.  
  147.  
  148. // ______________________________________________________________________
  149. // THE AE HANDLERS
  150.  
  151. // ______________________________________________________________________
  152. pascal OSErr AEOpenHandler(AppleEvent *theMessage, AppleEvent *theReply, long refCon)
  153. {
  154. // We are calling a stub function that supposedly will handle the open case (usually creating a new entity)
  155. // Default we do nothing.
  156.     return errAEEventNotHandled;
  157. }
  158.  
  159.  
  160. // ______________________________________________________________________
  161. pascal OSErr AEOpenDocHandler(AppleEvent *theMessage, AppleEvent *theReply, long refCon)
  162. {
  163. // Parse the incoming entries (could be more than one, and call a specific function for each incoming entry.
  164.     OSErr             anErr;
  165.     AEDescList    aDocumentList;
  166.     AEKeyword    aKeyword;
  167.     DescType        aTypeCode;
  168.     Size                actualSize;
  169.     long                nDocuments, index;
  170.     FSSpec            anFSSpec;
  171.     
  172.     anErr = AEGetParamDesc(theMessage, keyDirectObject, typeAEList, &aDocumentList); DebugAssert(anErr == noErr);
  173.     if(anErr != noErr) return anErr;
  174.     
  175.     anErr = CheckForRequiredAEParams(theMessage); DebugAssert(anErr == noErr);
  176.     if(anErr != noErr)
  177.     {
  178.         anErr = AEDisposeDesc(&aDocumentList); DebugAssert(anErr == noErr);
  179.          return anErr;
  180.     }
  181.     anErr = AECountItems(&aDocumentList, &nDocuments); DebugAssert(anErr == noErr);
  182.     if(anErr != noErr) 
  183.     {
  184.         anErr = AEDisposeDesc(&aDocumentList); DebugAssert(anErr == noErr);
  185.         return anErr;
  186.     }
  187.     
  188.     for(index = 1; index <= nDocuments; index++)
  189.     {
  190.         anErr = AEGetNthPtr(&aDocumentList, index, typeFSS, &aKeyword, &aTypeCode,(Ptr)&anFSSpec,
  191.                                             sizeof(FSSpec), &actualSize); DebugAssert(anErr == noErr);
  192.         if(anErr != noErr)
  193.             return anErr;
  194.         
  195.         // @@@ IF YOU NEED TO DO ANYTHING PER EACH FILE PASSED, DO IT HERE.
  196.         anErr = TestFunction(&anFSSpec); DebugAssert(anErr == noErr);
  197.         if(anErr != noErr)
  198.             return anErr;
  199.     }
  200.     
  201.     if(gOneShot)
  202.         gDone = true;
  203.     
  204.     anErr = AEDisposeDesc(&aDocumentList); DebugAssert(anErr == noErr);
  205.     
  206.     return noErr;
  207. }
  208.  
  209.  
  210. // ______________________________________________________________________
  211. pascal OSErr AEPrintHandler(AppleEvent *theMessage, AppleEvent *theReply, long refCon)
  212. {
  213. // We are calling a stub function that supposedly will handle the print case (usually printing a known entity)
  214. // Default we do nothing.
  215.     return errAEEventNotHandled;
  216. }
  217.  
  218.  
  219. // ______________________________________________________________________
  220. pascal OSErr AEQuitHandler(AppleEvent *theMessage, AppleEvent *theReply, long refCon)
  221. {
  222. // If we need to do any cleanup when quit:ing, do it here.
  223.     gDone = true;
  224.     
  225.     return noErr;
  226. }
  227.  
  228.  
  229. // ______________________________________________________________________
  230. // ADDITIONAL AE FUNCTIONS
  231.  
  232. // ______________________________________________________________________
  233. pascal OSErr CheckForRequiredAEParams(AppleEvent *theEvent)
  234. {
  235.     DescType    returnedType;
  236.     Size            actualSize;
  237.     OSErr        anErr;
  238.     
  239.     anErr = AEGetAttributePtr(theEvent, keyMissedKeywordAttr, typeWildCard, &returnedType, 
  240.                                                 NULL, 0, &actualSize); 
  241.     if(anErr == errAEDescNotFound)    // all the parameters were there!
  242.         return noErr;
  243.     else
  244.         if(anErr == noErr)                        // missed parameters
  245.             return errAEParamMissed;
  246.         else
  247.             return anErr;                        // the call to AEGetAttributePtr failed
  248. }